﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Common.Views;
using TokenGenerator;

namespace DataAccess
{
    public class UsersRepository: ConnectionClass
    {
        public UsersRepository() : base() { }

        public UsersRepository(bool isAdmin) : base(isAdmin) { }

        public User GetUser(string username)
        {
            return Entity.Users.SingleOrDefault(u => u.Username.ToLower() == username.ToLower());
        }

        public bool AuthenticateUser(string username, string token, int pin)
        {

            User u = GetUser(username.ToLower()); 
            if (u != null)
            {
                string password = new Encryption().DecryptTripleDES(token, pin.ToString());
                if ((u.Username.ToLower() == username.ToLower()) && (u.Password == password) && (u.PinNumber == pin))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        public bool DoesUserNameExist(string username)
        {
            if (Entity.Users.Count(u => u.Username.ToLower() == username.ToLower()) == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public bool DoesEmailExist(string email)
        {
            if (Entity.Users.Count(u => u.Email == email) == 0)
            {
                return false;
            }
            else return true;
        }

        public bool DoesPinExist(int pinNo)
        {
            if(Entity.Users.Count(u => u.PinNumber == pinNo) == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public void AddUser(User myNewUser)
        {
            Entity.Users.AddObject(myNewUser);
            Entity.SaveChanges();
        }

        public string GetUserEmail(string username)
        {
            return (
                from u in Entity.Users
                where u.Username == username
                select u.Email
                ).SingleOrDefault();
        }

        public IQueryable<UsersView> GetUsernames()
        {
            return (
                    from u in Entity.Users
                    select new UsersView()
                    {
                        Username = u.Username
                    }
                );
        }

        public string GetUserEmailByOrder(Guid OrderID)
        {
            return (
                from u in Entity.Users
                join o in Entity.Orders
                on u.Username equals o.Username
                where o.OrderID == OrderID
                select u.Email
                ).SingleOrDefault();
        }

        public string GetUserNumberByOrder(Guid OrderID)
        {
            return (
                from u in Entity.Users
                join o in Entity.Orders
                on u.Username equals o.Username
                where o.OrderID == OrderID
                select u.MobileNo
                ).SingleOrDefault();
        }
    }
}
